<?php
function trip_experiences($type, $nid, $profile) {
  global $user;
  $module_path = drupal_get_path("module", "trip");
  drupal_add_js($module_path."/js/trip.js");
  module_load_include('inc', 'node', 'node.pages');

  if(empty($type)) {
    $type = 'basic';
  }

  if(empty($nid) && $type != 'basic') {
    drupal_add_css($module_path . "/css/exp_basic.css");
    drupal_goto("experiences/basic");
  }

  // media tab
  if($type == 'media') {
    $module_path = drupal_get_path('module', 'trip');
    drupal_add_js($module_path . "/js/media.js");
    $node = node_load($nid);
//    print_r($node->field_exp_photos[LANGUAGE_NONE]);exit;
    return theme('trip_page_media', array('items' => array('node'=>$node)));
  }

  // profile tab
  if($type == 'profile') {
    if(empty($profile)) {
      $profile = 'basic';
    }
    drupal_add_css($module_path."/css/exp_profile.css");
    return trip_edit_profile($profile, 'exp');
  }

  // payment tab
  if($type == 'payment') {
    return trip_edit_payment();
    $user = user_load($user->uid);
    return theme('trip_page_payment', array('items' => $user));
  }

  // publish tab
  if($type == 'publish') {
    $node = node_load($nid);

    if($_POST['publish'] == 1) {
      $node->field_approval[LANGUAGE_NONE][0]['value'] = 2;
      $node->status = 1;
      node_save($node);
    }

    $items['status'] = $node->field_approval[LANGUAGE_NONE][0]['value'];

    return theme('trip_page_publish', array('items'=>$items));
  }

  if(empty($nid)) {
    $content = new stdClass;
    $content->type = 'exp_basic';
    $content->uid = $user->uid;
    $content->name = $user->name;
  }else{
    $content = node_load($nid);
  }

  $exp_form = drupal_get_form("exp_".$type."_node_form", $content);

  if($type == 'basic') {
//    unset($exp_form['field_exp_photos']);
    unset($exp_form['field_exp_video']);
    drupal_add_css($module_path . "/css/exp_basic.css");
  }else if($type == 'media') {
//    unset($exp_form['title']);
//    unset($exp_form['body']);
    foreach($exp_form as $key => $item) {
      if(preg_match("/^field_.*/", $key) && $key != 'field_exp_video' && $key != 'field_exp_photos') {
//        unset($exp_form[$key]);
      }
    }
  }
//  $node_obj = $exp_form['#node'];
//  $node_obj->name = 'renk';
//  $exp_form['#node'] = $node_obj;
  return $exp_form;
  return drupal_render($exp_form);


}

function format_basic_form(&$exp_form, $type = 'add') {
  if($type == 'add'){
    unset($exp_form['field_exp_photos']);
    unset($exp_form['field_exp_video']);
  }else{
    unset($exp_form['title']);
    unset($exp_form['body']);
    foreach($exp_form as $key => $item) {
      if(preg_match("/^field_.*/", $key) && $key != 'field_exp_video' && $key != 'field_exp_photos') {
        unset($exp_form[$key]);
      }
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function trip_form_alter(&$form, &$form_state, $form_id) {
  $form_state['redirect'] = 'experiences/payment/6';
  switch ($form_id) {
    case 'user_profile_form':
    case 'exp_media_node_form':
    case 'exp_basic_node_form':
      if(!user_access('administer') && arg(0) == 'experiences') {
        unset($form['actions']['delete']);
        $form['actions']['submit']['#value'] = t("Next");
        $form['actions']['submit']['#attributes'] =  array('class' => array('btn','btn-large red-btn','btn-primary'));
        $form['#submit'][] = 'trip_form_redirect';
      }
      break;
  }
}

function trip_node_insert($node) {
  global $user;
  if(arg(1) == 'basic') {
    $product = sync_product($node);
    $node->field_product[LANGUAGE_NONE][0]['product_id'] = $product->product_id;
    $node->is_new = false;
    $node->status = 0;
    node_save($node);
    drupal_goto('experiences/media/' . $node->nid);
  }
}

function trip_node_update($node) {
  if(arg(0) == 'experiences') {
    db_update("field_data_field_approval")->fields(array('field_approval_value'=>2))->condition("entity_id", $node->nid)->execute();
  }
  if(arg(1) == 'basic') {
    sync_product($node);
    drupal_goto('experiences/media/' . $node->nid);
  }else if(arg(1) == 'media' && arg(3) != 'delete'){
    drupal_goto('experiences/profile/'.arg(2));
  }
}


function trip_form_redirect() {
  if(arg(0) == 'experiences') {
    if(arg(1) == 'profile') {
        drupal_goto('experiences/payment/'.arg(2));
    }
    else if(arg(1) == 'payment'){
        drupal_goto('experiences/publish/'.arg(2));
    }
  }
}

function trip_experience_view($nid) {
  global $user;
  $node = node_load($nid);
  $account = user_load($node->uid);
  $data['node'] = $node;
  $data['user'] = $account;
  $data['comments'] = exp_get_reviews_list($nid);
  $product_id = $node->field_product[LANGUAGE_NONE][0]['product_id'];
  $data['product'] = commerce_product_load($product_id);
  return theme("trip_page_experience_view", array('items' => $data));
}

function trip_experiences_media_save($nid) {
  $node = node_load($nid);
  $node->field_exp_video[LANGUAGE_NONE][0]['value'] = check_plain($_POST['video_url']);
  node_save($node);
  drupal_goto("experiences/profile/".$nid);
}

function trip_experiences_media_delete($nid, $index) {
  $node = node_load($nid);
  unset($node->field_exp_photos[LANGUAGE_NONE][$index]);
  node_save($node);
  drupal_goto("experiences/media/".$nid);
}

function sync_product($node) {
  if(empty($node->field_product[LANGUAGE_NONE][0]['product_id'])) {
    $product = commerce_product_new('product');
  }else{
    $product = commerce_product_load($node->field_product[LANGUAGE_NONE][0]['product_id']);
  }
  $product->sku = 'trip-'.rand(5,10).'-'.$node->nid;
  $product->title = $node->title;
  $product->uid = $node->uid;
  $product->commerce_price[LANGUAGE_NONE][0]['amount'] = $node->field_price[LANGUAGE_NONE][0]['value']*100;
  $product->commerce_price[LANGUAGE_NONE][0]['currency_code'] = $node->field_currency[LANGUAGE_NONE][0]['value'];
  commerce_product_save($product);
  return $product;
}
